package com.estore;

import com.estore.service.ProductService;
import java.util.Scanner;

public class ConsoleShop {
    private ProductService productService;
    private Scanner scanner;

    public ConsoleShop() {
        this.productService = new ProductService();
        this.scanner = new Scanner(System.in);
    }

    public void start() {
        System.out.println("=== 商城系统 ===");
        System.out.println("数据持久化功能已启用");

        boolean running = true;
        while (running) {
            showMenu();
            int choice = getMenuChoice();

            switch (choice) {
                case 1:
                    browseAllProducts();
                    break;
                case 2:
                    addNewProduct();
                    break;
                case 3:
                    updateProductStock();
                    break;
                case 4:
                    // 退出前保存数据
                    exitAndSave();
                    running = false;
                    break;
                default:
                    System.out.println("无效选择，请重新输入!");
            }
        }
    }

    private void showMenu() {
        System.out.println("\n请选择操作:");
        System.out.println("1. 浏览所有商品");
        System.out.println("2. 添加新商品");
        System.out.println("3. 更新商品库存");
        System.out.println("4. 退出系统");
        System.out.print("请输入选择 (1-4): ");
    }

    private int getMenuChoice() {
        try {
            return Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    private void browseAllProducts() {
        productService.displayAllProducts();
    }

    private void addNewProduct() {
        System.out.println("\n=== 添加新商品 ===");
        System.out.print("输入商品ID: ");
        String id = scanner.nextLine();
        System.out.print("输入商品名称: ");
        String name = scanner.nextLine();
        System.out.print("输入商品价格: ");
        double price = Double.parseDouble(scanner.nextLine());
        System.out.print("输入商品库存: ");
        int stock = Integer.parseInt(scanner.nextLine());

        com.estore.model.Product newProduct =
                new com.estore.model.Product(id, name, price, stock);
        productService.addProduct(newProduct);

        System.out.println("新商品已添加到内存，退出时会自动保存到文件");
    }

    private void updateProductStock() {
        System.out.println("\n=== 更新商品库存 ===");
        productService.displayAllProducts();
        System.out.print("输入要更新的商品ID: ");
        String id = scanner.nextLine();
        System.out.print("输入新的库存数量: ");
        int newStock = Integer.parseInt(scanner.nextLine());

        productService.updateProductStock(id, newStock);
        System.out.println("库存已更新，退出时会自动保存到文件");
    }

    /**
     * 退出并保存数据
     */
    private void exitAndSave() {
        System.out.println("\n正在保存数据...");
        productService.saveData();
        System.out.println("感谢使用商城系统，再见!");
        scanner.close();
    }

    public static void main(String[] args) {
        ConsoleShop shop = new ConsoleShop();
        shop.start();
    }
}